package cn.neusoft.dao.impl;

import cn.neusoft.bean.Orderdetailet;
import cn.neusoft.bean.Orders;
import cn.neusoft.dao.IOrdersDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.OrdersQuery;
import cn.neusoft.utils.JdbcUtils;
import cn.neusoft.vo.ResultBean;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersDao implements IOrdersDao {
    private QueryRunner queryRunner;
public OrdersDao(){
    queryRunner= JdbcUtils.getQueryRunner();
}
    @Override
    public int add(Orders orders) throws SQLException {
       String sql="insert into t_orders(orderId, userId, businessId, orderDate, orderTotal, daId, orderState) VALUES (?,?,?,?,?,?,?)";
        return queryRunner.update(sql,
                orders.getOrderId(),
                orders.getUserId(),
                orders.getBusinessId(),
                orders.getOrderDate(),
                orders.getOrderTotal(),
                orders.getDaId(),
                orders.getOrderState());
    }

    @Override
    public int update(Orders orders) throws SQLException {
    String sql="update userId=?, businessId=?, orderDate=?, orderTotal=?, daId=?, orderState=? where orderId=?)";

    return queryRunner.update(sql,
            orders.getUserId(),
            orders.getBusinessId(),
            orders.getOrderDate(),
            orders.getOrderTotal(),
            orders.getDaId(),
            orders.getOrderState(),
            orders.getOrderId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql="delete from t_orders where OrderId=?";

        return queryRunner.update(sql,id);
    }

    @Override
    public Orders getById(Integer id) throws SQLException {
    String sql="select orderId, userId, businessId, orderDate, orderTotal, daId, orderState from t_orders where orderId=?";
    return queryRunner.query(sql,new BeanHandler<Orders>(Orders.class),id);
    }

    @Override
    public List<Orders> getAll() throws SQLException {
        String sql="select orderId, userId, businessId, orderDate, orderTotal, daId, orderState from t_orders";
        return queryRunner.query(sql,new BeanListHandler<>(Orders.class));
    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        OrdersQuery query= (OrdersQuery)baseQuery;
        List<Object> params=new ArrayList<>();
        String sql="select count(orderId) from t_orders where 1=1";
        if(query.getOrderId()!=null){
            sql+=" and orderId like concat('%',?,'%')";
            params.add(query.getOrderId());
        }  if(query.getOrderDate()!=null){
            sql+=" and orderDate like concat('%',?,'%')";
            params.add(query.getOrderDate());
        }
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);
        return queryRunner.query(sql, new ScalarHandler<Long>(), params.toArray()).intValue();
    }


    @Override
    public List<Orders> getByPage(BaseQuery baseQuery) throws SQLException {
        OrdersQuery query= (OrdersQuery)baseQuery;
        List<Object> params=new ArrayList<>();
        String sql="select orderId, userId, businessId, orderDate, orderTotal, daId, orderState where 1=1 ";
        if(query.getOrderId()!=null){
            sql+=" and orderId like concat('%',?,'%')";
            params.add(query.getOrderId());
        }  if(query.getOrderDate()!=null){
            sql+=" and orderDate like concat('%',?,'%')";
            params.add(query.getOrderDate());
        }
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);
        return queryRunner.query(sql, new BeanListHandler<Orders>(Orders.class), params.toArray());
    }
}
